﻿#region Copyright Info

// 
// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

#region

using System.Windows.Controls;
using System.Windows;

#endregion

namespace Majesty_of_Omega.GUI.Controller
{
    /// <summary>
    /// Base class for the model view controller pattern
    /// </summary>
    public class Controller : DependencyObject
    {

        /// <summary>
        /// Called when the [page changed].
        /// </summary>
        /// <param name="page">The page.</param>
        protected virtual void OnPageChanged(Page page)
        { }

        /// <summary>
        /// Set the corresponding wpf page for the controller
        /// </summary>
        /// <remarks>
        /// The setting of new page for this controller raises the <see cref="OnPageChanged" /> event
        /// </remarks>
        public Page Page
        {
            set
            {
                if (page != value)
                {
                    page = value;
                    OnPageChanged(value);
                }
            }
            get
            {
                return page;
            }

        }

        /// <summary>
        /// this is the corresponding wpf page of the controller
        /// </summary>
        private Page page;

        /// <summary>
        /// Initializes a new instance of the <see cref="Controller"/> class.
        /// </summary>
        /// <param name="page">The page.</param>
        public Controller(Page page)
        {
            this.page = page;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Controller"/> class. 
        /// the page must be set later with the page property
        /// </summary>
        public Controller()
        {
        }

        /// <summary>
        /// Navigate to a new page. The XAML-Page is a view in the model view controller context
        /// </summary>
        /// <param name="newPage">The new page.</param>
        protected void Navigate(Page newPage)
        {
            if (page != null && page.NavigationService != null)
            {
                page.NavigationService.Navigate(newPage);
            }
        }

        /// <summary>
        /// Called when the [page initialized].
        /// </summary>
        /// <param name="page">The page.</param>
        protected virtual void OnPageInitialized(Page page)
        {

        }

        /// <summary>
        /// The WPF Page tells the controller, that all controls are initalized
        /// </summary>
        /// <param name="page">The page, which invoked this call</param>
        public void PageInitialized(Page page)
        {
            OnPageInitialized(page);
        }
    }
}